/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.common.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.dao.ISysrefcodeDao;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.util.BlankUtil;

/**
 * 功能描述：系统参数列表持久化DAO
 * @author 罗军林 创建日期：2008-5-8
 */
public class SysrefcodeDaoImpl extends PmsBaseDaoImpl<SysrefcodeDO> implements ISysrefcodeDao{
	//只根据CODE查询
	private final String QUERY_SYSREFCODES_BY_CODE_HQL="from SysrefcodeDO A where A.active='1' and A.code=? order by A.seq";
	//根据CODE与VALUE组合查询
	private final String QUERY_SYSREFCODES_BY_CODE_STR_HQL="from SysrefcodeDO A where A.active='1' and A.code=? and A.value in(:filterStr) order by A.seq";

	/**
	 * 根据CODE与ID组合查询<br>
	 * :selectIdStr 应是一个形如：<b>1,2,3</b> 的字符串
	 */
	private final String QUERY_SYSREFCODES_BY_ID_STR_HQL="FROM SysrefcodeDO A WHERE A.active='1' AND A.code=? and A.id in(:selectIdStr) order by A.seq";

	public List<SysrefcodeDO> querySysrefCodesByCode(String code) {
		Query query=super.createQuery(QUERY_SYSREFCODES_BY_CODE_HQL);
		query.setString(0, code);
		return query.list();
	}
	/**
	 * 功能描述:查询所有指定的系统参数表<br>
	 * @param code
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-4  <br>
	 */
	public List<SysrefcodeDO> querySysrefCodesByCode(List<String> code){
		StringBuffer queryStr = new StringBuffer("FROM SysrefcodeDO sysrefCode ");
					 queryStr.append(" WHERE sysrefCode.active='1' ");
					 queryStr.append(" AND sysrefCode.code IN(:codes)");
		Query query = super.createQuery(queryStr.toString(), null);
		query.setParameterList("codes", code);

		return query.list();
	}
	public SysrefcodeDO getSysrefCodesByCode(String code, String value) {
		Criteria criteria_code = super.crtieriaFindBy("code", code);
		criteria_code.add(Restrictions.eq("value", value));
		return (SysrefcodeDO) this.findUnique(criteria_code);
	}

	public List querySysrefCodesByCode(String code, String filterStr) {
		String hql=QUERY_SYSREFCODES_BY_CODE_STR_HQL.replace(":filterStr", filterStr);
		Query query=super.createQuery(hql);
		query.setString(0, code);
		return query.list();
	}

	/**
	 *
	 * 功能描述: 根据code和选中的一个id的拼接字符串查询一个SysrefcodeDO的列表
	 *
	 * @author 付湘辉
	 * Created on:2008-7-4
	 *
	 * 修改原因：
	 * @param code
	 * @param selectIdStr
	 * @return List 返回结果
	 */
	public List<SysrefcodeDO> querySysrefCodesBySelectId(String code, String selectIdStr) {

		if (BlankUtil.isBlank(selectIdStr)) {
			return new ArrayList<SysrefcodeDO>();
		}

		if (selectIdStr.endsWith(SysInfo.SEPARATOR_COMMA)) {
			selectIdStr = selectIdStr.substring(0,selectIdStr.length()-1);
		}

		if (BlankUtil.isBlank(selectIdStr)) {
			return new ArrayList<SysrefcodeDO>();
		}

		String hql=QUERY_SYSREFCODES_BY_ID_STR_HQL.replace(":selectIdStr", selectIdStr);

		Query query=super.createQuery(hql);

		query.setString(0, code);

		return (List<SysrefcodeDO>)query.list();
	}

}
